package com.bkck.sharkblockade.levels.block;

import com.bkck.sharkblockade.levels.ball.Ball;
import com.bkck.sharkblockade.levels.lines.Line;
import android.graphics.Bitmap;
import android.graphics.Canvas;

/**
 * Block class
 * 
 * @author Team BKCK
 */
public class Block {
  
  // block colors
  public static final int RED = 3;
  public static final int YELLOW = 2;
  public static final int GREEN = 1;
  public static final int GRAY = -1;
  public static final int BLACK = -2;
    
  // blocks left to break
  public static int blocksToBreak = 0;
  
  private int color; 
  // lives for this block
  private int hitsLeft;
  private float x;
  private float y;
  private Bitmap bitmap;
  private int width;
  private int height;
  
  private Line leftEdge;
  private Line rightEdge;
  private Line topEdge;
  private Line bottomEdge;
  
  
  /**
   * The block constructor.
   * @param bitmap The bitmap image
   * @param color The color
   * @param x The x coordinate
   * @param y THe y coordinate
   */
  public Block(Bitmap bitmap, int color, float x, float y) {
    this.bitmap = bitmap;
    // values for color and hitsLeft are identical, just made two variables to avoid confusion.
    this.color = color;
    this.hitsLeft = color;
    this.x = x;
    this.y = y;
    this.width = bitmap.getWidth();
    this.height = bitmap.getHeight();
    
    if (color != GRAY) {
      blocksToBreak++;
    }
  }
  
  public void applyEdges(Line leftEdge, Line rightEdge, Line topEdge, Line bottomEdge) {
    this.leftEdge = leftEdge;
    this.rightEdge = rightEdge;
    this.topEdge = topEdge;
    this.bottomEdge = bottomEdge;
  }
  
  public Line getLeftEdge() {
    return leftEdge;
  }
  
  public Line getRightEdge() {
    return rightEdge;
  }
  
  public Line getTopEdge() {
    return topEdge;
  }
  
  public Line getBottomEdge() {
    return bottomEdge;
  }
      
  /**
   * Decrements hits left to break the block and changes its color.
   */
  public void gotHit() {
    this.hitsLeft -= 1;
    this.color -= 1;    
  }
  
  /**
   * Return the hits left to break this block.
   * @return hitLeft.
   */
  public int getHitsLeft() {
    return hitsLeft;
  }
  
  /**
   * Return the color.
   * @return color
   */
  public int getColor() {
    return color;
  }
  
  /**
   * Set x coordinate for this block.
   * @param x coordinate
   */
  public void setX(float x) {
    this.x = x;
  }
  
  /**
   * Return x coordinate for this block.
   * @return x coordinate.
   */
  public float getX() {
    return x;
  }
  
  /**
   * Set y coordinate for this block.
   * @param y coordinate
   */
  public void setY(float y) {
    this.y = y;
  }
  
  /**
   * Return y coordinate for this block.
   * @return y coordinate.
   */
  public float getY() {
    return y;
  }
  
  /**
   * Set the bitmap for this block.
   * @param bitmap The bitmap.
   */
  public void setBitmap(Bitmap bitmap) {
    this.bitmap = bitmap;
  }
  
  /**
   * Return the bitmap for this block.
   * @return bitmap
   */
  public Bitmap getBitmap() {
    return bitmap;
  }
  
  /**
   * Return the width for this block in pixels.
   * @return The width in pixels.
   */
  public int getWidth() {
    return width;
  }
  
  /**
   * Return the height for this block in pixels.
   * @return The height in pixels.
   */
  public int getHeight() {
    return height;
  }
  
  /**
   * Draw the block to the canvas.
   * 
   * @param canvas
   */
  public void draw(Canvas canvas) {
    canvas.drawBitmap(bitmap, x, y, null);
  }
  
  /**
   * Return whether this block and the ball have a collision.
   * @param ball The ball
   * @return True if collision was detected.
   */
  public boolean collideWith(Ball ball) {
    // collide from left
    if (ball.getX() + ball.getWidth() >= x && ball.getX() + ball.getWidth() <= x + width) {
      // collide from bottom left
      if (ball.getY() >= y && ball.getY() <= y + height) {
        return true;
      }
      // collide from top left
      if (ball.getY() + ball.getHeight() >= y && ball.getY() + ball.getHeight() <= y + height) {
        return true;
      }
    }
    // collide from right
    if (ball.getX() >= x && ball.getX() <= x + width) {
      // collide from bottom right
      if (ball.getY() >= y && ball.getY() <= y + height) {
        return true;
      }
      // collide from top right
      if (ball.getY() + ball.getHeight() >= y && ball.getY() + ball.getHeight() <= y + height) {
        return true;
      }
    }    
    return false;
  }
  

}
